package br.cin.ufpe.corba.examples.navigator;


import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import br.cin.ufpe.corba.ORB;

public class NamingServiceNavigator {
	
    public static void main( String[] argv ) throws NumberFormatException, IOException {        

        ORB orb = ORB.init(1137, "localhost", 1133);
        
        JFrame frame = new JFrame( "Naming Service Navigator" );
        frame.addWindowListener( new WindowAdapter() {
                                     public void windowClosing( WindowEvent e ) {
                                         System.exit( 0 );
                                     }
                                 });
        
        final NamingServiceModel model = new NamingServiceModel(orb);
        NamingServiceTreePanel tree = new NamingServiceTreePanel( model );
        JScrollPane treeScroller = new JScrollPane( tree );
                
        final JPanel panel = new JPanel(new BorderLayout());
        
        final JButton refresh = new JButton("Refresh");
        refresh.addMouseListener(new MouseAdapter(){
        	@Override
        	public void mouseClicked(MouseEvent e) {
        		model.refresh();
        		panel.removeAll();
        		
        		NamingServiceTreePanel newTree = new NamingServiceTreePanel( model );
                JScrollPane newTreeScroller = new JScrollPane( newTree );
                panel.add(newTreeScroller, BorderLayout.CENTER);
                panel.add(refresh, BorderLayout.SOUTH);
                
                panel.setVisible(false);
                panel.setVisible(true);
        	}
        });
                
        panel.add(treeScroller, BorderLayout.CENTER);
        panel.add(refresh, BorderLayout.SOUTH);
        
        frame.getContentPane().add( panel );
        frame.setSize( 800, 600 );
        frame.setVisible(true);
    }
}
